﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace game
{
    public class RollDogdeMovement
    {
        public float RollSpeed { get; set; }
        public float DodgeSpeed { get; set; }
        public float LeanStablePointEpsilon { get; set; }

        public bool Finished { get; private set; }

        private float dirSign;
        private bool firstHalf;

        public enum DodgeDir
        {
            LEFT,
            RIGHT,
        }

        public RollDogdeMovement()
        {
            RollSpeed = 0.007f;
            DodgeSpeed = 8.0f;
            LeanStablePointEpsilon = 0.1f;
        }

        public void Start(DodgeDir dir)
        {
            dirSign = (dir == DodgeDir.LEFT) ? 1 : -1;
            Finished = false;
            firstHalf = true;
        }

        public float RotateZ(float curRot)
        {
            curRot += dirSign * RollSpeed * MovementControl.DeltaTimeMs;
            if (Math.Abs(curRot) > MathHelper.Pi)
            {
                curRot -= Math.Sign(curRot) * MathHelper.TwoPi;
                firstHalf = false;
            }
            if (Math.Abs(curRot) < LeanStablePointEpsilon && !firstHalf)
            {
                curRot = 0.0f;
                Finished = true;
            }
            return curRot;
        }

        public Vector3 Dodge(Vector3 curPos, float curRotY)
        {
            Vector3 dodgeMove = Vector3.Transform(Vector3.Left, Matrix.CreateRotationY(curRotY));
            Vector3.Multiply(ref dodgeMove,
                DodgeSpeed * MovementControl.DeltaTimeMs * dirSign, out dodgeMove);
            Vector3.Add(ref curPos, ref dodgeMove, out curPos);
            return curPos;
        }
    }
}
